﻿@using Fuse8.ContentManagementFramework.SystemFacade.Extensions
@using Fuse8.ContentManagementFramework.Web.ApplicationRuntime.ContentModuleModels
@model dynamic

@{
    int pageControlId = (int)Model.PageControlId;
    var contentModel = (Model.Model as ContentModuleModel) ?? new ContentModuleModel();
    if (contentModel.Sources == null)
    {
        contentModel.Sources = new[] { new ContentModuleDataSource() { Where = new WhereModel(), Pager = new PagerModel() } };
    }
    ResFile = "cms_Pages";
    int index = 0;
}

@helper WhereOption(WhereType selected, WhereType render){
    <option value="@render" @(selected == render?"selected=\"selected\"":"")>@_(render.GetDescription())</option>
}

<ol class="ul-reset g-clear">
<li class="g-clear">
<h3>@_("Data Source")</h3>
</li>
@foreach (var d in contentModel.Sources)
{
    if (d.Pager == null)
    {
        d.Pager = new PagerModel();
    }
    <li class="b-form__row g-clear js-cm-source-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Name")">@_("Name"):</label> 
        <input type="text" class="text" value="@d.Name" name="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Name")"/>
    </li>
    <li class="b-form__row g-clear js-cm-source-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].ContentTypeId")">@_("Content type"): </label>
        @Html.DropDownList(Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].ContentTypeId"), contentModel.ContentTypes(d.ContentTypeId), "Please select")
    </li>
    <li class="b-form__row g-clear js-cm-source-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Where.Type")">@_("Where type"): </label>
        <select class="js-select-show-hide" data-container=".js-where-@index" data-prefix="w@(index)-" name="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Where.Type")">
            @WhereOption(d.Where.Type, WhereType.None)
            @WhereOption(d.Where.Type, WhereType.Const)
            @WhereOption(d.Where.Type, WhereType.QueryString)
            @WhereOption(d.Where.Type, WhereType.MvcRouteData)
            @WhereOption(d.Where.Type, WhereType.Session)
        </select>
    </li>
    <li class="b-form__row g-clear js-cm-source-@index js-where-@index js-w@(index)-const js-w@(index)-querystring js-w@(index)-mvcroutedata js-w@(index)-session">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Where.ParameterName")">@_("Field name"): </label>
        @Html.DropDownList(Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Where.ParameterName"), contentModel.ContentFields(d.ContentTypeId, d.Where.ParameterName), "Please select", new {@class="select js-fields-"+index})        
    </li>
    <li class="b-form__row g-clear js-cm-source-@index js-where-@index js-w@(index)-const js-w@(index)-querystring js-w@(index)-mvcroutedata js-w@(index)-session">
        <label class="js-where-@index js-w@(index)-const" for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Where.Value")">@_("Value"): </label>
        <input type="text" class="text" value="@d.Where.Value" name="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Where.Value")"/>
    </li>
    <li class="b-form__row g-clear js-cm-source-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].OrderField")">@_("Order field"): </label>
        @Html.DropDownList(Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].OrderField"), contentModel.ContentFields(d.ContentTypeId, d.OrderField), _("Please select"), new {@class="select js-fields-"+index})                
    </li>
    <li class="b-form__row g-clear js-cm-source-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Desc")">@_("Descending"): </label>
        <input id="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Desc")" type="checkbox" value="true" @(d.Desc ? "checked=\"checked\"" : "") name="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Desc")"/>
    </li>
    <li class="b-form__row g-clear js-cm-source-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.Enabled")">@_("Include Pager"):</label>
        <input class="js-checkbox-show-hide" data-container=".js-pager-@index" id="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.Enabled")" type="checkbox" value="true" @(d.Pager.Enabled ? "checked=\"checked\"" : "") name="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.Enabled")"/>
    </li>
    <li class="b-form__row g-clear js-cm-source-@index js-pager-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.PageSize")">@_("Page Size"): </label>
        <input type="text" class="text" value="@d.Pager.PageSize" name="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.PageSize")"/>
    </li>
    <li class="b-form__row g-clear js-cm-source-@index js-pager-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.VisibleLinkCount")">@_("Visible pages"): </label>
        <input type="text" class="text" value="@d.Pager.VisibleLinkCount" name="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.VisibleLinkCount")"/>
    </li>
    <li class="b-form__row g-clear js-cm-source-@index js-pager-@index">
        <label for="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.PageKey")">@_("Page key"): </label>
        <input type="text" class="text" value="@d.Pager.PageKey" name="@Html.GetClientNameForAdminViewElement(pageControlId, "Sources[" + index + "].Pager.PageKey")"/>
    </li>
    index++;
}
<li>
    <a data-index="@index" href="#" class="js-add-cm-source">@_("Add new source")</a>
</li>
<li class="g-clear">
<h3>@_("Template (Handlebars by default)")</h3>
</li>
<li class="g-clear">
<textarea name="@Html.GetClientNameForAdminViewElement(pageControlId, "Template")" cols="100" rows="15">@contentModel.Template</textarea>
</li>
</ol>

<script type="text/javascript">
    $(document).ready(function () {
        $(".js-add-cm-source").live("click", function (e) {
            e.preventDefault();
            var clone = $(".js-cm-source-0").clone();
            clone.removeClass(".js-cm-source-0");
            var i = $(this).data("index");
            clone.find("input:text, select").val("");
            clone.find("input:checkbox").attr("checked", false);
            clone.find("[name*=Sources]").each(function () {
                this.name = this.name.replace("[0]", "[" + i + "]");
            });
            i++;
            $(this).data("index", i);
            $(this).closest("li").before(clone);
        });
    });
</script>

